/**
 * 
 */
package com.demo2do.strawberry.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.strawberry.entity.investor.Institution;
import com.demo2do.strawberry.entity.user.UserRegister;
import com.demo2do.strawberry.entity.user.member.Member;
import com.demo2do.strawberry.handler.FileHandler;
import com.demo2do.strawberry.service.InstitutionService;
import com.demo2do.strawberry.service.InvestorService;
import com.demo2do.strawberry.service.UserService;

/**
 * @author lute
 *
 */
@Controller
@RequestMapping("/investors")
public class InvestorController {
	
	@Autowired
	private InstitutionService institutionService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private FileHandler fileHandler;
		
	@Autowired
	private InvestorService investorService;
	
	/**
	 * @param member
	 * @param userRegister
	 * @param institution
	 * @param name
	 * @param businessCard
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST)
	public String onCreate(Member member, UserRegister userRegister, @Preloading("institutionId") Institution institution, @RequestParam("name") String name, @RequestParam("businessCard") MultipartFile businessCard) throws Exception {
		
		// save businessCard
		String path = null;
		if(businessCard != null && !businessCard.isEmpty()) {
			path = fileHandler.save(businessCard.getOriginalFilename(), businessCard.getInputStream(), true);
		}
		
		investorService.createInvestor(member, userRegister, institution, name, path);
		
		return "redirect:/signup/activate";
	}

}
